# install Flask : pip install Flask
from flask import Flask, request, jsonify
from flask_cors import CORS
import subprocess
import os

app = Flask(__name__)
CORS(app)  # Pour gérer les requêtes CORS

@app.route('/run-downloader', methods=['POST'])
def run_downloader():
    result = request.json.get('result')
    if result:
        exe_path = os.path.join('Tools', 'N_m3u8DL-RE.exe')
        command = f'cmd /c "{exe_path}" " {result}"'
        try:
            subprocess.run(command, shell=True, check=True)
            return jsonify({'status': 'success'})
        except subprocess.CalledProcessError as e:
            return jsonify({'status': 'error', 'message': str(e)}), 500
    return jsonify({'status': 'error', 'message': 'URL manquante'}), 400

if __name__ == '__main__':
    app.run(port=5000)  # Vous pouvez changer le port si nécessaire
